<?php

namespace app\api\controller;

use think\queue\Job;
use think\facade\Db;
use app\common\library\Sms as Smslib;
use app\common\controller\Ding;

/**
 * 数据修改控制器
 */
class Queue
{
    /**
     * 报表
     */
    public function fire(Job $job, $data)
    {
        file_put_contents('./public/yd_callback/' . date("Ymd") . "BJyd.txt", json_encode($data) . PHP_EOL, FILE_APPEND);
        $job->delete();
        $this->doTask($data);
//        $job->delete();
        // file_put_contents('3.txt', 'data');
//        $isJobDone = $this->doTask($data);
//        if ($isJobDone) {
//            $job->delete();
//        } else {
//            $job->release(2);
//        }

    }

    /**
     *
     * task具体执行逻辑
     */
    public function doTask($data)
    {
        try {
            $mobile   = $data['mobile'];
            $dataInfo = Db::name('user_data')->where('mobile', $mobile)->where('sku_code', $data['prod_code'])->order('id desc')->limit(1)->find();
            if ($dataInfo) {
                $data['bus_code']   = $dataInfo['bus_code'];
                $data['pid']        = $dataInfo['pid'];
                $data['order_time'] = date("Y-m", $dataInfo['create_time']);
                $data['month_time'] = date('Y-m');
            }
            $res = Db::name('bjyd_order')->insert($data);
            if (empty($res)) {
                $title   = '北京移动Queue方法队列异常';
                $ding    = new Ding();
                $content = json_encode($data);
                $ding->dingSend($title, $content);
            }
            $startTime = microtime(true);
            //判断user_equity用户权益表是否存在这条数据存在的话就是修改结束时间不存在的话就是添加
            $find = Db::name('user_equity')->where('mobile', $mobile)->where('sku_code', $data['prod_code'])->where('type', 4)->find();
            if ($find) {
                $expire            = strtotime(date('Y-m-d', time() + 31 * 60 * 60 * 24));
                $da['update_time'] = time();
                $da['end_time']    = $expire;
                Db::name('user_equity')->where('id', $find['id'])->update($da);
            } else {
                $da['mobile']     = $mobile;
                $da['name']       = '平安健康权益包';
                $da['sku_code']   = $data['prod_code'];
                $da['start_time'] = time();
                $expire           = strtotime(date('Y-m-d', time() + 31 * 60 * 60 * 24));
                $da['end_time']   = $expire;
                if ($dataInfo) {
                    $da['bus_code'] = $dataInfo['bus_code'];
                    $da['pid']      = $dataInfo['pid'];
                }
                $da['type'] = 4;
                Db::name('user_equity')->insert($da);
            }

            Db::commit();
            Smslib::send((string)$mobile, $data['prod_code'],'', 11, 'follow');
            $endTime       = microtime(true);
            $executionTime = $endTime - $startTime;
            echo "队列[{$mobile}]执行结束,运行时长[{$executionTime}]" . PHP_EOL;
            // 提交事务
//            return true;
        } catch (\Exception $e) {
            Db::rollback();
            $title   = '接口异常';
            $content = $e->getMessage() . $data['mobile'] . '北京移动Queue方法队列异常';
            $ding    = new Ding();
            $ding->dingSend($title, $content);
            // 回滚事务
        }
        return true;
    }

    /*
     *	获取下个月的今天
     *	如果 $date = 2018-1-31 二月没有31号 则获取二月份的最后一天 返回值为2018-2-28
     *	如果 $date = 2018-1-15 返回值为2018-2-15
     *
     *	-- psw--
     */

    public function getNextMonthDays($date)
    {
        $firstday       = date('Y-m-01', strtotime($date));
        $lastday        = strtotime("$firstday +2 month -1 day");
        $day_lastday    = date('d', $lastday); //获取下个月份的最后一天
        $day_benlastday = date('d', strtotime("$firstday +1 month -1 day")); //获取本月份的最后一天
        //获取当天日期
        $Same_day = date('d', strtotime($date));
        //判断当天是否是最后一天   或 下月最后一天 等于 本月的最后一天
        if ($Same_day == $day_benlastday || $day_lastday == $Same_day) {
            $day = $day_lastday;
        } else {
            $day = $Same_day;
        }
        $day = date('Y', $lastday) . '-' . date('m', $lastday) . '-' . $day;
        return $day;
    }

    /**
     * 获取随机数
     */
    public function getRandomString($len)
    {
        $str    = null;
        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        $max    = strlen($strPol) - 1;
        for ($i = 0; $i < $len; $i++) {
            $str .= $strPol[rand(0, $max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }
        //判断数据库是否存在这个卡密
        // $isFind = CdkeyModel::where('coupon_code_psd',$str)->find();
        // if($isFind){
        // 	strtoupper($this->getRandomString(8));
        // }
        return $str;
    }
}
